package com.library.service;

import com.library.dao.BookDAO;
import com.library.dao.BorrowDAO;
import com.library.dao.UserDAO;
import com.library.model.Book;
import com.library.model.Borrow;
import com.library.model.User;

import java.util.Date;
import java.util.List;

public class BorrowService {

    private final BorrowDAO borrowDAO = new BorrowDAO();
    private final UserDAO userDAO = new UserDAO();
    private final BookDAO bookDAO = new BookDAO();

    public void addBorrow(Borrow borrow) {
        List<User> users = userDAO.findUserByCondition(borrow.getUsername(), null, null, null);
        if (users == null || users.isEmpty()) {
            throw new RuntimeException("借阅人不存在");
        }
        List<Book> books = bookDAO.findByCondition(null, null, borrow.getIsbn(), null);
        if (books == null || books.isEmpty()) {
            throw new RuntimeException("图书不存在");
        }
        borrowDAO.add(borrow);
    }

    public List<Borrow> findByCondition(String username, String bookTitle, String status) {
        List<Borrow> borrows = borrowDAO.findByCondition(username, bookTitle, status);
        return borrows;
    }

    public void deleteById(int id) {
        borrowDAO.delete(id); // 调用DAO执行删除
    }

    public void update(Borrow borrow) {
        borrowDAO.update(borrow);

    }

    public void backBookById(int id) {
        Date returnDate = new Date(); // 实际归还日期（当前系统时间）
        borrowDAO.updateReturnStatus(id, returnDate);
    }
}
